home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / SHLExamples / Delegation / EOFDelegateControllerCategory.m < prev    next >
Encoding:
Text File  |  1994-12-31  |  16.5 KB  |  674 lines

  1. /*--------------------------------------------------------------------------
  2.  *
  3.  *     You may freely copy, distribute, and reuse the code in this example.
  4.  *     SHL Systemhouse disclaims any warranty of any kind, expressed or  
  5.  *    implied, as to its fitness for any particular use.
  6.  *
  7.  *
  8.  *    Controller
  9.  *
  10.  *    Category Of:    EOFDelegate
  11.  *
  12.  *    Conforms To:    None
  13.  *
  14.  *    Declared In:    EOFDelegateControllerCategory.h
  15.  *
  16.  *
  17.  *------------------------------------------------------------------------*/
  18. #import "EOFDelegateControllerCategory.h"
  19.  
  20.  
  21.  
  22.  
  23. @implementation EOFDelegate (Controller)
  24.  
  25. /*--------------------------------------------------------------------------
  26.  *    EOController Delegate Methods
  27.  *
  28.  *  Note that the insert, delete, and update operations are buffered until an
  29.  *  -saveToDataSource message is sent to the controller.  The delegate receives
  30.  *  delegate messages for these operations as the insert, delete, and update
  31.  *  operations are sent to the controller.  The delegate receives different
  32.  *  delegate messages when the buffered operations are actually sent on to the
  33.  *  data source.
  34.  *
  35.  *  typedef enum {
  36.  *    EONoDataSourceFailure = 0,
  37.  *    EOContinueDataSourceFailureResponse,
  38.  *    EORollbackDataSourceFailureResponse
  39.  *  } EODataSourceFailureResponse;
  40.  *
  41.  *  These return values allow a controller's delegate to determine what
  42.  *  action should be taken on a failure.  EONoDataSourceFailure means there
  43.  *  was no failure; EOContinuteDataSourceFailureResponse means that the
  44.  *  failure should be ignored and the operation should continue;
  45.  *  EORollbackDataSourceFailureResponse means that the controller should
  46.  *  roll back the data source.  EORollbackDataSourceFailureResponse is only
  47.  *  useful if the data source conforms to the EORollbackDataSource
  48.  *  protocol.
  49.  *
  50.  *------------------------------------------------------------------------*/
  51. - (void)controllerDidChangeSelection:(EOController *)controller
  52. {
  53.     // Sent when the selection changes.
  54.     
  55.     [[NXApp delegate] announce:controller selector:_cmd];
  56. }
  57.  
  58.     
  59. - (BOOL)controllerWillFetch:(EOController *)controller
  60. {
  61.     // Informs the delegate that the controller is about to fetch.  If the
  62.     // delegate returns no the fetch fails; if the delegate returns YES
  63.     // the fetch proceeds.
  64.     
  65.     BOOL    result = YES;
  66.     
  67.     [[NXApp delegate] announce:controller selector:_cmd];
  68.     
  69.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  70.         {
  71.         switch (NXRunAlertPanel ("EOController", 
  72.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  73.             {
  74.             case NX_ALERTALTERNATE:
  75.                 result = NO;
  76.                 break;
  77.             case NX_ALERTDEFAULT:
  78.             default:
  79.                 result = YES;
  80.                 break;
  81.             }
  82.         }
  83.     
  84.     return result;
  85. }
  86.  
  87.  
  88. - (void)controller:(EOController *)controller didFetchObjects: (NSArray *)objects
  89. {
  90.     // Informs the delegate that the controller has fetched object.
  91.     
  92.     [[NXApp delegate] announce:controller 
  93.         selector:_cmd 
  94.         with:[NSArray arrayWithObject:objects]];
  95. }
  96.  
  97.  
  98. - (BOOL)controller:(EOController *)controller 
  99.     willInsertObject:object 
  100.     atIndex: (unsigned)newIndex
  101. {
  102.     // If the delegate returns NO object is released and not inserted.
  103.     
  104.     BOOL    result = YES;
  105.     
  106.     [[NXApp delegate] announce:controller 
  107.         selector:_cmd 
  108.         with:[NSArray arrayWithObject:object]];
  109.     
  110.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  111.         {
  112.         switch (NXRunAlertPanel ("EOController", 
  113.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  114.             {
  115.             case NX_ALERTALTERNATE:
  116.                 result = NO;
  117.                 break;
  118.             case NX_ALERTDEFAULT:
  119.             default:
  120.                 result = YES;
  121.                 break;
  122.             }
  123.         }
  124.     
  125.     return result;
  126. }
  127.  
  128.  
  129. - (void)controller:(EOController *)controller didInsertObject:object
  130. {
  131.     // Informs the delegate that the controller has inserted object.
  132.     
  133.     [[NXApp delegate] announce:controller 
  134.         selector:_cmd 
  135.         with:[NSArray arrayWithObject:object]];
  136. }
  137.  
  138.  
  139. - (BOOL)controller:(EOController *)controller willDeleteObject:object
  140. {
  141.     // If the delegate returns NO object is not deleted.
  142.     
  143.     BOOL    result = YES;
  144.     
  145.     [[NXApp delegate] announce:controller 
  146.         selector:_cmd 
  147.         with:[NSArray arrayWithObject:object]];
  148.     
  149.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  150.         {
  151.         switch (NXRunAlertPanel ("EOController", 
  152.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  153.             {
  154.             case NX_ALERTALTERNATE:
  155.                 result = NO;
  156.                 break;
  157.             case NX_ALERTDEFAULT:
  158.             default:
  159.                 result = YES;
  160.                 break;
  161.             }
  162.         }
  163.     
  164.     return result;
  165. }
  166.  
  167.  
  168. - (void)controller:(EOController *)controller didDeleteObject:object
  169. {
  170.     // Informs the delegate that the controller has deleted object.
  171.     
  172.     [[NXApp delegate] announce:controller 
  173.         selector:_cmd 
  174.         with:[NSArray arrayWithObject:object]];
  175. }
  176.  
  177.  
  178. - (NSDictionary *)controller:(EOController *)controller 
  179.     willSaveEdits: (NSDictionary *)edits 
  180.     toObject:object
  181. {    
  182.     // Invoked by -saveObjects for each object before
  183.     // -takeValuesFromDictionary: is sent to the object.  If the delegate
  184.     // responds nil -takeValuesFromDictionary: is not sent and the controller
  185.     // aborts -saveToObjects.  The edits will be ignored and the associations
  186.     // will not be notified of any change.  If the delegate returns a
  187.     // dictionary that dictionary will be sent to the object.  This method is
  188.     // useful for doing validation prior to data actually being sent to the
  189.     // object and will also prevent the UI from refreshing.
  190.     
  191.     id    result = edits;
  192.     
  193.     [[NXApp delegate] announce:controller 
  194.         selector:_cmd 
  195.         with:[NSArray arrayWithObjects: edits, object, nil]];
  196.     
  197.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  198.         {
  199.         switch (NXRunAlertPanel ("EOController", 
  200.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  201.             {
  202.             case NX_ALERTALTERNATE:
  203.                 result = nil;
  204.                 break;
  205.             case NX_ALERTDEFAULT:
  206.             default:
  207.                 result = edits;
  208.                 break;
  209.             }
  210.         }
  211.     
  212.     return result;
  213. }
  214.  
  215.  
  216. - (void)controller:(EOController *)controller didSaveToObject:object
  217. {
  218.     // Informs the delegate that the controller has saved object.
  219.     
  220.     [[NXApp delegate] announce:controller 
  221.         selector:_cmd 
  222.         with:[NSArray arrayWithObject:object]];
  223. }
  224.  
  225.  
  226. /*--------------------------------------------------------------------------
  227.  *    NOTE
  228.  *    
  229.  *    The following two methods have been commented out to allow natural
  230.  *    processing of associated events.  See comments below for these methods.
  231.  *
  232.  
  233. - (BOOL)controllerWillDiscardEdits:(EOController *)controller
  234. {
  235.     // Sent during the -fetch method before the fetch begins if there are any
  236.     // operations that haven't been saved to the objects.  Also sent when a
  237.     // selection is about to change, causing a detail controller to flush its
  238.     // caches.  The operation is aborted if this method returns NO.  The
  239.     // delegate should send -saveToObjects if it wants to preserve the
  240.     // changes.  If the delegate doesn't implement this method the controller
  241.     // opens an alert panel warning the user that edits may be lost.
  242.     
  243.     BOOL    result = YES;
  244.     
  245.     [[NXApp delegate] announce:controller selector:_cmd];
  246.     
  247.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  248.         {
  249.         switch (NXRunAlertPanel ("EOController", 
  250.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  251.             {
  252.             case NX_ALERTALTERNATE:
  253.                 result = NO;
  254.                 break;
  255.             case NX_ALERTDEFAULT:
  256.             default:
  257.                 result = YES;
  258.                 break;
  259.             }
  260.         }
  261.     
  262.     return result;
  263. }
  264.  
  265.  
  266. - (BOOL)controllerWillDiscardOperations:(EOController *)controller
  267. {
  268.     // Sent during the -fetch method before the fetch begins if there are any
  269.     // operations that haven't been saved to the data source.  Also sent
  270.     // when a selection is about to change, causing a detail controller to
  271.     // flush its caches.  The fetch is aborted if this method returns NO.
  272.     // The delegate should invoke -saveToDataSource if it wants to preserve the
  273.     // changes.  If the delegate doesn't implement this method the controller
  274.     // opens an alert panel warning the user that updates may be lost.
  275.     
  276.     BOOL    result = YES;
  277.     
  278.     [[NXApp delegate] announce:controller selector:_cmd];
  279.     
  280.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  281.         {
  282.         switch (NXRunAlertPanel ("EOController", 
  283.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  284.             {
  285.             case NX_ALERTALTERNATE:
  286.                 result = NO;
  287.                 break;
  288.             case NX_ALERTDEFAULT:
  289.             default:
  290.                 result = YES;
  291.                 break;
  292.             }
  293.         }
  294.     
  295.     return result;
  296. }
  297.  *------------------------------------------------------------------------*/
  298.  
  299.  
  300.  
  301. - (BOOL)controllerWillSaveToDataSource:(EOController *)controller
  302. {
  303.     // These delegate messages are sent as the data source is notified of
  304.     // changes to data-bearing objects.  If the savesToDataSourceAutomatically
  305.     // flag is turned off and -saveToDataSource is invoked then the delegate
  306.     // is notified of each buffered operation.
  307.     
  308.     BOOL    result = YES;
  309.     
  310.     [[NXApp delegate] announce:controller selector:_cmd];
  311.     
  312.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  313.         {
  314.         switch (NXRunAlertPanel ("EOController", 
  315.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  316.             {
  317.             case NX_ALERTALTERNATE:
  318.                 result = NO;
  319.                 break;
  320.             case NX_ALERTDEFAULT:
  321.             default:
  322.                 result = YES;
  323.                 break;
  324.             }
  325.         }
  326.     
  327.     return result;
  328. }
  329.  
  330.  
  331. - (void)controllerDidSaveToDataSource:(EOController *)controller
  332. {
  333.     // These delegate messages are sent as the data source is notified of
  334.     // changes to data-bearing objects.  If the savesToDataSourceAutomatically
  335.     // flag is turned off and -saveToDataSource is invoked then the delegate
  336.     // is notified of each buffered operation.
  337.  
  338.     [[NXApp delegate] announce:controller selector:_cmd];
  339. }
  340.  
  341.  
  342. - (EODataSourceOperationDelegateResponse)controller:(EOController *)controller
  343.     willInsertObject:object
  344.     inDataSource:dataSource
  345. {
  346.     int    result = EOPerformDataSourceOperation;
  347.     
  348.     [[NXApp delegate] announce:controller 
  349.         selector:_cmd 
  350.         with:[NSArray arrayWithObjects: object, dataSource, nil]];
  351.     
  352.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  353.         {
  354.         switch (NXRunAlertPanel ("EOController", 
  355.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  356.             {
  357.             case NX_ALERTALTERNATE:
  358.                 result = EODiscardDataSourceOperation;
  359.                 break;
  360.             case NX_ALERTDEFAULT:
  361.             default:
  362.                 result = EOPerformDataSourceOperation;
  363.                 break;
  364.             }
  365.         }
  366.     
  367.     return result;
  368. }
  369.  
  370.  
  371. - (EODataSourceFailureResponse)controller:(EOController *)controller
  372.     failedToInsertObject:object
  373.     inDataSource:dataSource
  374. {
  375.     int    result = EONoDataSourceFailure;
  376.     
  377.     [[NXApp delegate] announce:controller 
  378.         selector:_cmd 
  379.         with:[NSArray arrayWithObjects: object, dataSource, nil]];
  380.     
  381.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  382.         {
  383.         switch (NXRunAlertPanel ("EOController", 
  384.             "%s", "No Failure", "Continue", "Rollback", sel_getName(_cmd)))
  385.             {
  386.             case NX_ALERTOTHER:
  387.                 result = EORollbackDataSourceFailureResponse;
  388.                 break;
  389.             case NX_ALERTALTERNATE:
  390.                 result = EOContinueDataSourceFailureResponse;
  391.                 break;
  392.             case NX_ALERTDEFAULT:
  393.             default:
  394.                 result = EONoDataSourceFailure;
  395.                 break;
  396.             }
  397.         }
  398.     
  399.     return result;
  400. }
  401.  
  402.  
  403. - (void)controller:(EOController *)controller
  404.     didInsertObject:object
  405.     inDataSource:dataSource
  406. {
  407.     [[NXApp delegate] announce:controller 
  408.         selector:_cmd 
  409.         with:[NSArray arrayWithObjects: object, dataSource, nil]];
  410. }
  411.  
  412.  
  413. - (EODataSourceOperationDelegateResponse)controller:(EOController *)controller
  414.     willDeleteObject:object
  415.     inDataSource:dataSource
  416. {
  417.     int    result = EOPerformDataSourceOperation;
  418.     
  419.     [[NXApp delegate] announce:controller 
  420.         selector:_cmd 
  421.         with:[NSArray arrayWithObjects: object, dataSource, nil]];
  422.     
  423.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  424.         {
  425.         switch (NXRunAlertPanel ("EOController", 
  426.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  427.             {
  428.             case NX_ALERTALTERNATE:
  429.                 result = EODiscardDataSourceOperation;
  430.                 break;
  431.             case NX_ALERTDEFAULT:
  432.             default:
  433.                 result = EOPerformDataSourceOperation;
  434.                 break;
  435.             }
  436.         }
  437.     
  438.     return result;
  439. }
  440.  
  441.  
  442. - (EODataSourceFailureResponse)controller:(EOController *)controller
  443.     failedToDeleteObject:object
  444.     inDataSource:dataSource
  445. {
  446.     int    result = EONoDataSourceFailure;
  447.     
  448.     [[NXApp delegate] announce:controller 
  449.         selector:_cmd 
  450.         with:[NSArray arrayWithObjects: object, dataSource, nil]];
  451.     
  452.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  453.         {
  454.         switch (NXRunAlertPanel ("EOController", 
  455.             "%s", "No Failure", "Continue", "Rollback", sel_getName(_cmd)))
  456.             {
  457.             case NX_ALERTOTHER:
  458.                 result = EORollbackDataSourceFailureResponse;
  459.                 break;
  460.             case NX_ALERTALTERNATE:
  461.                 result = EOContinueDataSourceFailureResponse;
  462.                 break;
  463.             case NX_ALERTDEFAULT:
  464.             default:
  465.                 result = EONoDataSourceFailure;
  466.                 break;
  467.             }
  468.         }
  469.     
  470.     return result;
  471. }
  472.  
  473.  
  474. - (void)controller:(EOController *)controller
  475.     didDeleteObject:object
  476.     inDataSource:dataSource
  477. {
  478.     [[NXApp delegate] announce:controller 
  479.         selector:_cmd 
  480.         with:[NSArray arrayWithObjects: object, dataSource, nil]];
  481. }
  482.  
  483.  
  484. - (EODataSourceOperationDelegateResponse)controller:(EOController *)controller
  485.     willUpdateObject:object
  486.     inDataSource:dataSource
  487. {
  488.     int    result = EOPerformDataSourceOperation;
  489.     
  490.     [[NXApp delegate] announce:controller 
  491.         selector:_cmd 
  492.         with:[NSArray arrayWithObjects: object, dataSource, nil]];
  493.     
  494.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  495.         {
  496.         switch (NXRunAlertPanel ("EOController", 
  497.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  498.             {
  499.             case NX_ALERTALTERNATE:
  500.                 result = EODiscardDataSourceOperation;
  501.                 break;
  502.             case NX_ALERTDEFAULT:
  503.             default:
  504.                 result = EOPerformDataSourceOperation;
  505.                 break;
  506.             }
  507.         }
  508.     
  509.     return result;
  510. }
  511.  
  512.  
  513. - (EODataSourceFailureResponse)controller:(EOController *)controller
  514.     failedToUpdateObject:object
  515.     inDataSource:dataSource;
  516. {
  517.     int    result = EONoDataSourceFailure;
  518.     
  519.     [[NXApp delegate] announce:controller 
  520.         selector:_cmd 
  521.         with:[NSArray arrayWithObjects: object, dataSource, nil]];
  522.     
  523.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  524.         {
  525.         switch (NXRunAlertPanel ("EOController", 
  526.             "%s", "No Failure", "Continue", "Rollback", sel_getName(_cmd)))
  527.             {
  528.             case NX_ALERTOTHER:
  529.                 result = EORollbackDataSourceFailureResponse;
  530.                 break;
  531.             case NX_ALERTALTERNATE:
  532.                 result = EOContinueDataSourceFailureResponse;
  533.                 break;
  534.             case NX_ALERTDEFAULT:
  535.             default:
  536.                 result = EONoDataSourceFailure;
  537.                 break;
  538.             }
  539.         }
  540.     
  541.     return result;
  542. }
  543.  
  544.  
  545. - (void)controller:(EOController *)controller
  546.     didUpdateObject:object
  547.     inDataSource:dataSource
  548. {
  549.     [[NXApp delegate] announce:controller 
  550.         selector:_cmd 
  551.         with:[NSArray arrayWithObjects: object, dataSource, nil]];
  552. }
  553.  
  554.  
  555. - (void)controller:(EOController *)controller 
  556.     willRollbackDataSource:(id <EODataSources>)dataSource
  557. {
  558.     [[NXApp delegate] announce:controller 
  559.         selector:_cmd 
  560.         with:[NSArray arrayWithObject: dataSource]];
  561. }
  562.  
  563.  
  564. - (void)controller:(EOController *)controller 
  565.     didRollbackDataSource:(id <EODataSources>)dataSource
  566. {
  567.     [[NXApp delegate] announce:controller 
  568.         selector:_cmd 
  569.         with:[NSArray arrayWithObject: dataSource]];
  570. }
  571.  
  572.  
  573. - (BOOL)controllerWillUndo:(EOController *)controller
  574. {
  575.     BOOL    result = YES;
  576.     
  577.     [[NXApp delegate] announce:controller selector:_cmd];
  578.     
  579.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  580.         {
  581.         switch (NXRunAlertPanel ("EOController", 
  582.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  583.             {
  584.             case NX_ALERTALTERNATE:
  585.                 result = NO;
  586.                 break;
  587.             case NX_ALERTDEFAULT:
  588.             default:
  589.                 result = YES;
  590.                 break;
  591.             }
  592.         }
  593.     
  594.     return result;
  595. }
  596.  
  597.  
  598. - (void)controllerDidUndo:(EOController *)controller
  599. {
  600.     [[NXApp delegate] announce:controller selector:_cmd];
  601.     
  602. }
  603.  
  604.  
  605. - (void)controller:(EOController *)controller 
  606.     didChangeDataSource: (id <EODataSources>)dataSource
  607. {
  608.     [[NXApp delegate] announce:controller 
  609.         selector:_cmd 
  610.         with:[NSArray arrayWithObject: dataSource]];
  611. }
  612.  
  613.  
  614. - (void)controller:(EOController *)controller 
  615.     association: (EOAssociation *)association 
  616.     didEditObject: anObject 
  617.     key: (NSString *)key 
  618.     value: aValue
  619. {
  620.     [[NXApp delegate] announce:controller 
  621.         selector:_cmd 
  622.         with:[NSArray arrayWithObjects: 
  623.             association, anObject, key, aValue, nil]];
  624. }
  625.  
  626.  
  627. - (BOOL)controller:(EOController *)controller willUndoObject:anObject
  628. {
  629.     BOOL    result = YES;
  630.     
  631.     [[NXApp delegate] announce:controller 
  632.         selector:_cmd 
  633.         with:[NSArray arrayWithObject: anObject]];
  634.     
  635.     if ([[NXApp delegate] wantsAlertPanels] == YES)
  636.         {
  637.         switch (NXRunAlertPanel ("EOController", 
  638.             "%s", "YES", "NO", NULL, sel_getName(_cmd)))
  639.             {
  640.             case NX_ALERTALTERNATE:
  641.                 result = NO;
  642.                 break;
  643.             case NX_ALERTDEFAULT:
  644.             default:
  645.                 result = YES;
  646.                 break;
  647.             }
  648.         }
  649.     
  650.     return result;
  651. }
  652.  
  653.  
  654. - (void)controller:(EOController *)controller didUndoObject:anObject
  655. {
  656.     [[NXApp delegate] announce:controller 
  657.         selector:_cmd 
  658.         with:[NSArray arrayWithObject: anObject]];
  659. }
  660.  
  661. // This delegate method can be implemented to respond when the controller is 
  662. // unable to create an object for a data source. If the delegate does not 
  663. // implement this method, the controller will put up a panel to alert the user
  664. // of the error. Otherwise, the delegate is responsible for notifying the user. 
  665. - (void) controller:(EOController *)controller
  666.     createObjectFailedForDataSource:dataSource
  667. {
  668.     [[NXApp delegate] announce:controller 
  669.         selector:_cmd 
  670.         with:[NSArray arrayWithObject: dataSource]];
  671. }
  672.  
  673. @end
  674.